﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Product
{
    public class Photo
    {
        /// <summary>
        /// Return list of photo
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Product.Photo> GetListOfPhoto(int IDProduct)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR016_ListProductFlaggedFromProduct", 0, objListOfParameters);

                List<LimpaGaragem.Model.Product.Photo> objListOfPhoto = new List<LimpaGaragem.Model.Product.Photo>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Product.Photo objPhoto = new LimpaGaragem.Model.Product.Photo();
                    objPhoto.ID = IDProduct;
                    objPhoto.ID = Convert.ToInt32(objDataRow["ID_PHOTO"]);
                    objPhoto.Image = (byte[])objDataRow["IMG_PHOTO"];
                    objPhoto.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfPhoto.Add(objPhoto);
                }
                return objListOfPhoto;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get photo
        /// </summary>
        /// <returns></returns>
        public LimpaGaragem.Model.Product.Photo GetPhoto(int IDPhoto)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PHOTO", IDPhoto));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR012_SelectPhotoProduct", 0, objListOfParameters);

                LimpaGaragem.Model.Product.Photo objPhoto = null;
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    objPhoto = new LimpaGaragem.Model.Product.Photo();
                    objPhoto.ID = IDPhoto;
                    objPhoto.IDProduct = Convert.ToInt32(objDataRow["ID_PRODUCT"]);
                    objPhoto.Image = (byte[])objDataRow["IMG_PHOTO"];
                    objPhoto.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                }
                return objPhoto;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Insert new photo
        /// </summary>
        public int InsertPhoto(int IDProduct,
                               byte[] ImagePhoto)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PRODUCT", IDProduct));
                objListOfParameters.Add(new SqlParameter("@IMG_PHOTO", ImagePhoto));

                //EXECUTE
                return objDataAccess.ExecuteQueryAndReturnValue(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR012_InsertPhotoProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete photo
        /// </summary>
        public void DeletePhoto(int IDPhoto)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_PHOTO", IDPhoto));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR012_DeletePhotoProduct", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
